﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;

namespace trangbaocao.Controllers
{
    [Authorize]
    public class StationsController : Controller
    {
        //
        // GET: /Stations/

        StationsModel StationsModel = new StationsModel();
        
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            StationsAssociateModel StationsAssociateModel = new StationsAssociateModel();
            List<StationsAssociateModel> Stations = StationsAssociateModel.getStationsIndex();
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            return View(Stations);
        }
        KhuvucsModel KhuvucsModel = new KhuvucsModel();
        public ActionResult AddStations()
        {
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            StationsModel.trung_tam_id = new SelectList(main_stationModel, "id", "name");

            List<KhuvucsModel> khuvucModel = KhuvucsModel.getKhuvucsByMain_Stations(Convert.ToInt32(StationsModel.trung_tam_id.Skip(0).First().Value));
            StationsModel.khuvucs_id = new SelectList(khuvucModel, "makv", "ten_kv");

            return View(StationsModel);
        }
        [HttpPost]
        public ActionResult AddStations(StationsModel Stations)
        {
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            List<KhuvucsModel> khuvucModel = KhuvucsModel.getKhuvucsByMain_Stations(Convert.ToInt32(Stations.main_stations_id));
            if (ModelState.IsValid)
            {
                if (StationsModel.checkExist(Stations) > 0)
                {
                    TempData["msg"] = "Nhà trạm đã tồn tại!";                    
                    Stations.khuvucs_id = new SelectList(khuvucModel, "makv", "ten_kv");

                    Stations.trung_tam_id = new SelectList(main_stationModel, "id", "name");
                    return View(Stations);
                }
                StationsModel.tram_node_b = Convert.ToInt16(Request["tram_node_b"]); 
                StationsModel.addStations(Stations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";

            
            Stations.khuvucs_id = new SelectList(khuvucModel, "makv", "ten_kv");

            Stations.trung_tam_id = new SelectList(main_stationModel, "id", "name");
            return View(Stations);
        }
        public ActionResult EditStations(int id)
        {
            StationsModel StationsModel_ = StationsModel.getStations().Find(delegate(StationsModel s) { return s.id == id; });
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            StationsModel_.trung_tam_id = new SelectList(main_stationModel, "id", "name");

            List<KhuvucsModel> khuvucModel = KhuvucsModel.getKhuvucsByMain_Stations(Convert.ToInt32(StationsModel_.main_stations_id));
            StationsModel_.khuvucs_id = new SelectList(khuvucModel, "makv", "ten_kv");


            return View(StationsModel_);
        }
        [HttpPost]
        public ActionResult EditStations(StationsModel Stations)
        {
            List<Main_StationsModel> main_stationModel = Main_StationsModel.getMain_Stations();
            if (ModelState.IsValid)
            {
                StationsModel.tram_node_b = Convert.ToInt16(Request["tram_node_b"]); ;
                StationsModel.updateStations(Stations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";

            List<KhuvucsModel> khuvucModel = KhuvucsModel.getKhuvucsByMain_Stations(Convert.ToInt32(Stations.main_stations_id));
            Stations.khuvucs_id = new SelectList(khuvucModel, "makv", "ten_kv");

            Stations.trung_tam_id = new SelectList(main_stationModel, "id", "name");
            return View(Stations);
        }

        [HttpPost]
        public JsonResult getKhuvucsJson(string MainStationId, string Order)
        {
            return Json(KhuvucsModel.getKhuvucsByMain_Stations(MainStationId, Order));
        }
        public ActionResult DeleteStations(int id)
        {

            if (StationsModel.checkSubId(id) > 0 || StationsModel.checkSubId_Fuel_Input(id) > 0)
            {
                TempData["error"] = "Bạn không thể xóa do dữ liệu này đã được sử dụng!";
                return RedirectToAction("Index");
            }
            StationsModel.deleteStations(id);
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult CheckedAction(int[] Items)
        {
            foreach (int item in Items)
            {
                if (StationsModel.checkSubId(item) > 0 || StationsModel.checkSubId_Fuel_Input(item) > 0)
                {
                    TempData["error"] = "Bạn không thể xóa do có dữ liệu đã chọn đã được sử dụng!";
                    return RedirectToAction("Index");
                }
            }
            foreach (int item in Items)
            {
                StationsModel.deleteStations(item);
            }
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        public ActionResult FindStations()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult FindStations(string timkiem)
        {
            string trungtam_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            string s = "";
            ViewBag.selectedMain_stations = "";
            if (int.TryParse(Request.Form["main_stations_id"], out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " b.id=" + trungtam_id + " AND ";
                ViewBag.selectedMain_stations = Request.Form["main_stations_id"];
            }
            if (Session != null && s.ToString() == "")
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " b.id=" + trungtam_session_id + " AND ";
            }
            string sql;
            string dk = "";
            if (Request["select_option"] == "stations.ma_tram")
                dk = "a.ma_tram";
            else if (Request["select_option"] == "stations.name")
                dk = "a.name";            
            if (timkiem == "")
                sql = s + dk + " like '%" + timkiem + "%' or " + dk + " is null";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";
            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];
            StationsAssociateModel StationsAssociateModel = new StationsAssociateModel();
            List<StationsAssociateModel> Stations = StationsAssociateModel.FindStations(sql);
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();            
            if (ViewBag.selectedMain_stations.Length == 0)
            {
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            }
            else
            {
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name", ViewBag.selectedMain_stations);
                
            }
            return View("Index",Stations);
        }


        public ActionResult Stations_Report_Index()
        {
            return View("~/Views/Stations/Report/Stations_Report_Index.cshtml");
        }

    }
}
